package com.hzhk.happyrent.ui.holder.card;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;

import com.google.gson.Gson;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.rent.RentHeaderPagerBean;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.ViewPagerScroller;
import com.hzhk.happyrent.ui.adapter.viewpager.FocusImageAdapter;
import com.hzhk.happyrent.ui.holder.BaseViewHolder;
import com.hzhk.happyrent.ui.transformer.ScaleInTransformer;
import com.hzhk.happyrent.ui.view.viewpager.RollViewPager;
import com.tamic.novate.Throwable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by soft_zzc on 2017/11/24.
 */

public class CardHeaderViewHolder extends BaseViewHolder {

    @BindView(R.id.viewPager)
    RollViewPager viewPager;
    private Logger log = LoggerFactory.getLogger(this.getClass().getName());
    private RentHeaderPagerBean rentHeaderPagerBean;
    private Handler mHandler = new Handler();
    private static final int TIME = 8000;
    private boolean startRoll = true;
    Runnable runnableForViewPager = new Runnable() {

        @Override
        public void run() {
            try {
                if (startRoll) {
                    viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
                    mHandler.postDelayed(this, TIME);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    public CardHeaderViewHolder(View convertView, Context context) {
        log.debug("jump into CardHeaderViewHolder");
        super.view = convertView;
        super.context = context;
        ButterKnife.bind(this, view);
        initView();
        log.debug("jump out CardHeaderViewHolder");
    }
    private void initView() {
        log.debug("jump into initView");
        viewPager.setPageMargin(20);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setPageTransformer(true, new ScaleInTransformer());
        ViewPagerScroller pagerScroller = new ViewPagerScroller(context);
        pagerScroller.setScrollDuration(2000);// 设置时间，时间越长，速度越慢
        pagerScroller.initViewPagerScroll(viewPager);
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                switch (arg1.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                    case MotionEvent.ACTION_DOWN:
                        if (startRoll) {
                            mHandler.removeCallbacks(runnableForViewPager);
                            mHandler.removeCallbacksAndMessages(null);
                            startRoll = false;
                        }
                        break;
                    case MotionEvent.ACTION_UP:
//                        itemPosition = mViewPager.getCurrentItem() + 1;
                        mHandler.postDelayed(runnableForViewPager, TIME);
                        startRoll = true;
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
        getHomeBanner();
        log.debug("jump out initView");
    }

    private void getHomeBanner() {
        Map<String,Object> params=new HashMap<>();
        params.put("seat","2");
        HttpManager.getInstance().get(API.GET_SLIDERS, params, new MyStringResponse(context) {
            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                rentHeaderPagerBean=new Gson().fromJson(response,RentHeaderPagerBean.class);
                setData();
            }
        });
//
    }

    public void setData() {
        if(rentHeaderPagerBean!=null){
            FocusImageAdapter focusImageAdapter=new FocusImageAdapter(context,rentHeaderPagerBean.getData().getSliders());
            viewPager.setAdapter(focusImageAdapter);
            if (rentHeaderPagerBean.getData().getSliders().size() > 0) {
                startRoll = true;
                viewPager.setCurrentItem(rentHeaderPagerBean.getData().getSliders().size() * 20);
                viewPager.setCanScrolled(true);
                mHandler.removeCallbacksAndMessages(null);
                mHandler.removeCallbacks(runnableForViewPager);
                mHandler.postDelayed(runnableForViewPager, TIME);
            } else {
                mHandler.removeCallbacks(runnableForViewPager);
                viewPager.setCanScrolled(false);
                startRoll = false;
            }
        }
    }

}
